app.controller('mainContentCtrl', ['$rootScope', '$scope', '$state', '$http', '$filter', 'sweetAlertUtil', 'toaster', 'cookieUtil',
    function($rootScope, $scope, $state, $http, $filter, sweetAlertUtil, toaster, cookieUtil) {

        $scope.menuList = [];

        initMenu();

        function initMenu() {
            getMenu();
        }

        function getMenu() {
            $http({
                method: 'GET',
                url: "/api/menus"
            }).success(function(data){
                if (data.code !== 200) {
                    return;
                }
                $scope.menuList = data.result;
            });
        }

        function getCookie(c_name) {
            if (document.cookie.length > 0) {
                var c_start = document.cookie.indexOf(c_name + "=");
                if (c_start != -1) {
                    c_start = c_start + c_name.length + 1;
                    var c_end = document.cookie.indexOf(";", c_start);
                    if (c_end == -1) {
                        c_end = document.cookie.length;
                    }
                    return unescape(document.cookie.substring(c_start, c_end));
                }
            }
            return "";
        }

        /*  退出  */
        $scope.loginOut = function() {
            var token = cookieUtil.getCookie('token');
            if(token == '') {
                $state.go('login');
            } else {  
                $http({
                    method:'POST',
                    url: "/api/login/quit", 
                    headers : {'token' : token}
                }).success(function(data) {
                    if (data.code == 200 || data.code == 1) {
                        cookieUtil.delCookie('token');
                        $state.go('login');
                    } else {
                        sweetAlertUtil.fail(data.msg);
                    }
                }).error(function(data) {});
            }


            // $.post(BASE_URL + "login/quit", params, function(data, textStatus, xhr) {
            //     if (data.code === 200 || data.code === 1) {
            //         $state.go('login');
            //     } else {
            //         sweetAlertUtil.fail(data.msg);
            //     }
            // });
        };
        /* / 退出 */

        /*     当前日期  */
        var today = new Date();

        var weekNum = today.getDay(),
            weekArray = ["日", "一", "二", "三", "四", "五", "六"];

        var weekDay = '星期' + weekArray[weekNum];

        $scope.timeTip = $filter('date')(today, 'yyyy-MM-dd') + '  ' + weekDay;
        /*  /  当前日期  */

        /*  获取地区  */
        $scope.loc = '全部区域'

        function setLocation() {
            $http({
                method: "GET",
                url: BASE_URL + "areaManager/location"
            }).success(function(data) {
                if (data.code !== 200) {
                    $scope.loc = data.msg;

                    return;
                }
                $scope.loc = data.result.address;
            }).error(function() {
                $scope.loc = "地区加载失败";
            });
        }
        /*  / 获取地区  */

        /*  获取用户信息  */
        $scope.user = ''

        setUserInfos();

        function setUserInfos() {
            $http({
                method: "GET",
                url: BASE_URL + "login/userInfo"
            }).success(function(data) {
                if (data.code == 200) {
                    if(data.result.role_tag == "AREA"){
                        setLocation();
                    }
                }else{
                    $scope.user = data.msg;
                }

                $scope.user = data.result;
            }).error(function() {
                $scope.user = "用户信息加载失败";
            });
        }
        /*  / 获取用户信息  */
    }
]);
